using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SumGam.Globals;
using SumGam.Structs;

namespace SumGam
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
   
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        // Data Structures
        
        //public LoadMaps(string path)
        public Game1()
        {
            GameServices.Graphics = new GraphicsDeviceManager(this);
            GameServices.Content = Content;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            // TODO: Add your initialization logic here
            GameServices.Audio = new AudioEngine("Content\\SumGam.xgs");
            Audio.Sounds = new SoundBank(GameServices.Audio, "Content\\Sound Bank.xsb");
            Audio.Waves = new WaveBank(GameServices.Audio, "Content\\Wave Bank.xwb");
            GameServices.Graphics.PreferredBackBufferHeight = Settings.ScreenHeight;
            GameServices.Graphics.PreferredBackBufferWidth = Settings.ScreenWidth;
            GameServices.Graphics.ApplyChanges();
            Misc.Time = new GameTime();
            Misc.ScreenOrigin = new Vector2(480 / 2, 480 / 2);
            //PostProcessing.TitleScreenEffect = new BrightExtract(GraphicsDevice, TargetPlatform.Windows);
            //PostProcessing.TitleScreenEffect.Threshold = 0.5f;
            States.MajorState = new BaseState();
            States.MinorState = new BaseState();
            States.BlankState = new BaseState();
            States.TitleScreen = new TitleState("Press [enter]", new Vector2(150, 480 / 2+100));
            Helpers.SetMajorState(States.TitleScreen);
            Helpers.SetMinorState(States.BlankState);
            //this.IsFixedTimeStep = false;
            //Misc.lua.RegisterFunction("NewTimer", this, this.GetType().GetMethod("NewTimer"));
            //Misc.lua.GetFunction("Initialize").Call();
            base.Initialize();
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            Rendering.spriteBatch = new SpriteBatch(GraphicsDevice);
            Loading.CreateFonts(Content,GraphicsDevice);
            Loading.CreateTextures(Content,GraphicsDevice);
            Loading.CreateCreditsScreen(Content, GraphicsDevice);
            Loading.CreateMainMenu(Content, GraphicsDevice);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            //Misc.lua.GetFunction("UnloadContent").Call();
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //Misc.Ticks++; hey I should put these two in the game state because menus and dialogs dont need timers and shit
            //Timers.UpdateTimers();
            Misc.NewKeyboardState = Keyboard.GetState();
            Misc.Time = gameTime;
            if (States.MajorState.JustChanged)
            {
                States.MajorState.JustChanged = false;
            }
            else
            {
                States.MajorState.Update();
            }
            if (States.MinorState.JustChanged)
            {
                States.MinorState.JustChanged = false;
            }
            else
            {
                States.MinorState.Update();
            }
            Misc.OldKeyboardState = Misc.NewKeyboardState;
            GameServices.Audio.Update();
            // TODO: Add your update logic here
            //Misc.lua.GetFunction("Update").Call(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            Misc.Time = gameTime;
            GameServices.Graphics.GraphicsDevice.Clear(Color.Black);
            //Misc.lua.GetFunction("Draw").Call(gameTime);
            // TODO: Add your drawing code here
            States.MajorState.Draw();
            States.MinorState.Draw();
            base.Draw(gameTime);
        }
    }
}
